from recruit.adapter.stubs import StubUserGateway
from recruit.application.protocols import UoW
from recruit.application.protocols.interactor import Interactor
from recruit.domain.models.user import UserResult, UserUpdateDTO
from recruit.domain.services.user import UserService


class UpdateUser(Interactor[UserUpdateDTO, UserResult]):
    def __init__(
            self,
            uow: UoW,
            user_db_gateway: StubUserGateway,
            user_service: UserService
    ) -> None:
        self.uow = uow
        self.user_db_gateway = user_db_gateway
        self.user_service = user_service

    async def __call__(self, data: UserUpdateDTO) -> UserResult:
        self.user_service.check_user(
            await self.user_db_gateway.check_user(id=data.id)
        )
        user = await self.user_db_gateway.update(
            data.id,
            **self.user_service.update_user(data)
        )
        user = self.user_service.get_user(user)
        await self.uow.commit()
        return user
